/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.synjones.pay.bank.api;

import com.synjones.pay.model.common.ResponseBean;
import com.synjones.pay.model.context.IPayContext;
import com.synjones.pay.model.context.StatusInfo;
import java.util.Map;

/**
 *
 * @author zhzy
 */
public abstract class AbstractBankDirectorImpl implements IBankDirector {

    @Override
    public ResponseBean doPay(IPayContext ctx) {
        Map<String, String> reqMap = this.buildSendData(ctx);
        if (reqMap != null) {
            Map<String, String> resMap = this.sendData(reqMap);
            if (resMap != null && verifyRespSign(resMap)) {
                return this.resolveRespData(resMap);
            } else {
                return genFaildResponseBean(StatusInfo._4001.code());
            }
        } else {
            return genFaildResponseBean(StatusInfo._4000.code());
        }
    }

    @Override
    public abstract Map<String, String> sendData(Map<String, String> reqData);

    @Override
    public abstract Map<String, String> buildSendData(IPayContext ctx);

    @Override
    public abstract ResponseBean resolveRespData(Map<String, String> resMap);

    @Override
    public abstract boolean verifyRespSign(Map<String, String> resMap);

    /**
     * 生成失败bean
     *
     * @return
     */
    public ResponseBean genFaildResponseBean(String code) {
        ResponseBean.Builder builder = new ResponseBean.Builder();
        builder.statusCode(code);
        ResponseBean responseBean = builder.builder();
        return responseBean;
    }

}
